package com.manager.commons.encrypt.aes;

//                   _ooOoo_    
//                  o8888888o    
//                  88" . "88    
//                  (| -_- |)    
//                   O\ = /O    
//               ____/'---'\____    
//              /. '\\| : |//' .\    
//             // \\||| : |||// \\   
//            // _|||||-:-|||||_ \\    
//           // | | \\\ - /// | | \\
//           || |_| ''\---/'' |_| || 
//            \ .-\___/'-'\___/-. /    
//          ___\. .' /--.--\ '. ./___    
//        .""'< `.___\_<|>_/___.' >'"".    
//       | |: `- \`.|'\ _ /'|.`/ -` :| |    
//        \ \ `-. \_ __\ /__ _/ .-` / /    
//======'-.___`-.___\_______/___.-`___.-'======    
//                   '=---='    
//.................................................    
//写一哈代码，你就青叫唤；信龟儿设计，你逗是傻逼。
//永无Bug，啷个可能！求佛主保佑也没球用！

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * Aes
 * @author H
 * @version V1.0
 */
public class Aes {

	private final static String _character = "UTF-8";
	private final static String _ivp = "animal#cqlhlddwy";

	/**
	 * 加密
	 * @param text
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String text, String key) throws Exception {
		return encrypt(text, key, _ivp, _character);
	}

	/**
	 * 加密
	 * @param text
	 * @param key
	 * @param ivp
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String text, String key, String ivp) throws Exception {
		return encrypt(text, key, ivp, _character);
	}

	/**
	 * 加密
	 * @param text
	 * @param key
	 * @param ivp
	 * @param character
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String text, String key, String ivp, String character) throws Exception {
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		byte[] raw = keyLen16(key);
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(raw, "AES"), new IvParameterSpec(ivp.getBytes()));
		byte[] encrypted = cipher.doFinal(text.getBytes(_character));
		return new Base64().encodeToString(encrypted);// 此处使用BASE64做转码。
	}

	/**
	 * 解密
	 * @param text
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String text, String key) throws Exception {
		return decrypt(text, key, _ivp, _character);
	}

	/**
	 * 解密
	 * @param text
	 * @param key
	 * @param vip
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String text, String key, String ivp) throws Exception {
		return decrypt(text, key, ivp, _character);
	}

	/**
	 * 解密
	 * @param text
	 * @param key
	 * @param ivp
	 * @param character
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String text, String key, String ivp, String character) throws Exception {
		try {
			byte[] raw = keyLen16(key);
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(raw, "AES"), new IvParameterSpec(ivp.getBytes()));
			byte[] original = cipher.doFinal(new Base64().decode(text));
			return new String(original, character);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 
	 * @param key
	 * @return
	 */
	public static byte[] keyLen16(String key) {
		// 密钥补位
		byte[] arrBTmp = key.getBytes();
		byte[] arrB = new byte[16];

		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}
		return arrB;
	}
}